package com.mtt.thirdapi.yanxuan.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mtt.thirdapi.yanxuan.service.YXCallbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class YXCallbackServiceImpl implements YXCallbackService {

    public void orderCancel(String orderCancelResult) {
        JSONObject object = JSONObject.parseObject(orderCancelResult);
        String orderId = object.getString("orderId");
        Integer cancelStatus = object.getInteger("cancelStatus");
        String rejectReason = object.getString("rejectReason");
        log.info("订单取消回调  人工审核信息 orderId={}，cancelStatus={},rejectReason={}",
                orderId, cancelStatus, rejectReason);
    }

    public void orderExceptional(String exceptionInfo) {
        JSONObject object = JSONObject.parseObject(exceptionInfo);
        String orderId = object.getString("orderId");
        String reason = object.getString("reason");
        String extData = object.getString("extData");
        log.info("订单取消回调 订单异常回调信息 orderId={}，reason={},extData:{}",
                orderId, reason, extData);
    }

    public void orderDelivered(String orderPackage) {
        JSONObject object = JSONObject.parseObject(orderPackage);
        //向订单包裹插入数据
        String orderId = object.getString("orderId");//订单号
        Long packageId = object.getLong("packageId");//包裹号
        Long expCreateTime = object.getLong("expCreateTime");//发货时间，unix时间戳，单位毫秒

        //向订单包裹物流插入数据
        JSONArray expressDetailInfos = object.getJSONArray("expressDetailInfos");//物流详细信息（带上sku）

        //具体参考orderPackage.json
        log.info("订单包裹物流绑单回调信息 orderId={},packageId={},expCreateTime={}", orderId, packageId, expCreateTime);
        log.info("订单包裹物流绑单回调信息 物流详细信息 expressDetailInfos={}", expressDetailInfos);
    }

    public void refundAddress(String refundAddress) {
        JSONObject object = JSONObject.parseObject(refundAddress);
        String orderId = object.getString("orderId");
        String applyId = object.getString("applyId");
        Integer type = object.getInteger("type");
        String returnAddr = object.getString("returnAddr");
        log.info("退货地址回调信息 orderId={}，applyId={}，type={}", orderId, applyId, type);
        log.info("退货地址回调信息 returnAddr={}", returnAddr);
    }

    public void refundReject(String rejectInfo) {
        JSONObject object = JSONObject.parseObject(rejectInfo);
        String orderId = object.getString("orderId");
        String applyId = object.getString("applyId");
        String rejectReason = object.getString("rejectReason");
        log.info("严选拒绝退货回调信息，orderId={},applyId={},rejectReason={}", orderId, applyId, rejectReason);

    }

    public void refundExpressConfirm(String expressConfirm) {
        JSONObject object = JSONObject.parseObject(expressConfirm);
        String applyId = object.getString("applyId");//申请单Id
        String trackingCompany = object.getString("trackingCompany");//物流公司
        String trackingNum = object.getString("trackingNum");// 	物流单号
        Long trackingTime = object.getLong("trackingTime");//物流签收时间，单位为毫秒
        log.info("严选退货包裹确认收货回调信息：applyId={}，trackingCompany={}，trackingNum={}，trackingTime={}"
                , applyId, trackingCompany, trackingNum, trackingTime);

    }

    public void refundSystemCancel(String systemCancel) {
        JSONObject object = JSONObject.parseObject(systemCancel);
        String applyId = object.getString("applyId");
        String orderId = object.getString("orderId");
        String errorMsg = object.getString("errorMsg");//系统错误原因
        log.info("严选系统取消退货回调信息：applyId={},orderId={},errorMsg={}", applyId, orderId, errorMsg);

    }

    public void refundResult(String refundResult){
        JSONObject object = JSONObject.parseObject(refundResult);
        String applyId = object.getString("applyId");
        String orderId = object.getString("orderId");
        JSONArray refundSkuList = object.getJSONArray("refundSkuList");//退款的商品信息
        log.info("退款结果回调信息：applyId={},orderId={}", applyId, orderId);
        log.info("退款结果回调信息：refundSkuList={}", refundSkuList);
    }

    public void inventoryCountCheck(String skuCheck){
        JSONObject object = JSONObject.parseObject(skuCheck);
        String id = object.getString("id");//唯一标识符
        Long operateTime = object.getLong("operateTime");// 操作时间
        JSONArray skuChecks = object.getJSONArray("skuChecks");//Sku校准记录 skuId count当前库存
        log.info("渠道SKU库存校准回调信息 skuCheck={},skuChecks={}", id,operateTime);
        log.info("渠道SKU库存校准回调信息 skuChecks={}",skuChecks);

    }

}
